/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import com.hvtt.web.models.C_tuvanchovai;
import com.hvtt.web.models.Si_nganhang;
import com.hvtt.web.models.C_tuvanchovai;
import com.hvtt.web.models.Si_nganhang;
import com.hvtt.web.utilities.Utilities;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class C_TuVanChoVayController implements Serializable{
    private WEBConnection DAL=new WEBConnection();
    
    private C_tuvanchovai item =new C_tuvanchovai();
    
    public C_tuvanchovai getItem()
    {
        return item;
    }
    public void setItem(C_tuvanchovai item)
    {
        this.item=item;
    }
    
   
    
    public List<C_tuvanchovai> findAllTuVanChoVay() throws SQLException
    {
        WEBParamCollection Params=new WEBParamCollection();
        Params.add("GetAll");
        Params.add(this.item.getID());
        Params.add(this.item.getNganHangID());
        Params.add(this.item.getLoai());
        Params.add(this.item.getLaiXuat());
        Params.add(this.item.getDienGiai());
        Params.add(this.item.getPTThanhToanTruoc());
           
        List<C_tuvanchovai> lst= DAL.executeQuery("{call Proc_c_tuvanchovai_In_Up_Del(?,?,?,?,?,?,?)}",Command.sqlStoredProcedure,Params,C_tuvanchovai.class);
        return lst;        
    }
    public List<Si_nganhang> findAllNganHang() throws SQLException
    {
        
        List<Si_nganhang> lst= DAL.executeQuery("Select* from SI_NganHang", Si_nganhang.class);
        if(this.getItem().getNganHangID()<=0)
        {
            if(lst.size()>0)
            {
                item.setNganHangID(lst.get(0).getID());
            }
        }
        return lst;        
    }
    
     private String validateFields() throws SQLException {
        String str="";
        if (this.getItem().getLaiXuat()<=0)
        {
            str+="  + " + Utilities.getLanguages("LaiXuat")+": " + Utilities.getLanguages("Require") + "<br/>";
        }
       
        if("".equals(str.trim()))
            return "";
        return "<b style='color:black;'>:</b> <br/>" +  "<b style='color:red;'>" + str + "</b>";
    }
    
    
    
    public void btnSave_Click(ActionEvent event) throws IOException, SQLException {
       
        
         
          this.getItem().setDienGiai(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("DienGiai")));
          this.getItem().setLoai(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("Loai")));
          this.getItem().setNganHangID(Long.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("NganHangID")));
          
          this.getItem().setLaiXuat(Double.parseDouble(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("LaiXuat").trim()==""?"0":
                  FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("LaiXuat").trim()));
          this.getItem().setPTThanhToanTruoc(Double.parseDouble(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("PTThanhToanTruoc").trim()==""?"0":
                  FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("PTThanhToanTruoc").trim()));
          
           String result = this.validateFields();
      
            if (!result.isEmpty()) {
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status",result);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                RequestContext.getCurrentInstance().execute("update=\"growl\"");
                return;
          }
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("Save");
            Params.add(this.item.getID());
            Params.add(this.item.getNganHangID());
            Params.add(this.item.getLoai());
            Params.add(this.item.getLaiXuat());
            Params.add(this.item.getDienGiai());
            Params.add(this.item.getPTThanhToanTruoc());
            DAL.executeUpdate("{call Proc_c_tuvanchovai_In_Up_Del(?,?,?,?,?,?,?)}", Command.sqlStoredProcedure, Params);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/c/bankservice.html");
            
            this.item.resetAttribute();
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    
     public void btnDelete_Click(ActionEvent event) throws IOException, SQLException {
       
        
          this.getItem().setID(Long.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ID")));
         
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("Delete");
            Params.add(this.item.getID());
            Params.add(this.item.getNganHangID());
            Params.add(this.item.getLoai());
            Params.add(this.item.getLaiXuat());
            Params.add(this.item.getDienGiai());
            Params.add(this.item.getPTThanhToanTruoc());
            DAL.executeUpdate("{call Proc_c_tuvanchovai_In_Up_Del(?,?,?,?,?,?,?)}", Command.sqlStoredProcedure, Params);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/c/bankservice.html");
            
            this.item.resetAttribute();
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    public void btnUpdate_Click(ActionEvent event) throws IOException, SQLException {
       
        
          this.getItem().setID(Long.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ID")));
         
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("GetByKey");
           Params.add(this.item.getID());
            Params.add(this.item.getNganHangID());
            Params.add(this.item.getLoai());
            Params.add(this.item.getLaiXuat());
            Params.add(this.item.getDienGiai());
            Params.add(this.item.getPTThanhToanTruoc());
            
            List<C_tuvanchovai> lst= DAL.executeQuery("{call Proc_c_tuvanchovai_In_Up_Del(?,?,?,?,?,?,?)}", Command.sqlStoredProcedure, Params,C_tuvanchovai.class);
            if(lst!=null && lst.size()>0)
            {
                this.setItem(lst.get(0));
                FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/c/bankservice/new.html");
            }
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    
    public void btnNganHangID_Click(ActionEvent event) throws IOException, SQLException {
       
        
          this.getItem().setNganHangID(Long.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ID")));
          
         // RequestContext.getCurrentInstance().execute("update=\"pnTuVanChoVay\"");
          
         
    }
    
    public void btnTaiChinh_ChoVay_Change(ActionEvent event) throws IOException, SQLException {
       
        long lNganHang=Long.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("NganHangID"));
        
        String Loai=String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("Loai"));
        
        WEBParamCollection Params=WEBParamCollection.getInstance();
        Params.add(lNganHang);
        Params.add(Loai);
        List<C_tuvanchovai> lst=DAL.executeQuery("{call PG_Load_C_TuVanChoVay_NganHang(?,?)}", Command.sqlStoredProcedure, Params, C_tuvanchovai.class);
        if(lst.size()>0)
        {
            this.getItem().setLaiXuat(lst.get(0).getLaiXuat());
            
        }
        else
            this.getItem().resetAttribute();
        this.getItem().setNganHangID(lNganHang);
        this.getItem().setLoai(Loai);
    }   
    
    
}
